package com.zdj.presentation.modules.user.vms;

import android.databinding.ObservableBoolean;
import android.databinding.ObservableField;
import android.os.Bundle;
import android.view.View;

import com.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.zdj.aoplibrary.annotations.DebugTrace;
import com.zdj.data.datasource.UserCloudDataStore;
import com.zdj.data.entity.LoginInfo;
import com.zdj.data.entity.User;
import com.zdj.presentation.base.BaseLoadingViewModel;
import com.zdj.presentation.mappers.IEntity2Model;
import com.zdj.presentation.modules.user.adapters.UserAdapter;
import com.zdj.presentation.modules.user.models.UserModel;
import com.zdj.presentation.modules.user.views.UserActivity;
import com.zdj.presentation.modules.user.views.UserDetailActivity;
import com.zdj.utils.LogLevel;

/**
 * Created by zdj on 16-5-29.
 */

public class UserVm extends BaseLoadingViewModel<UserActivity> {

    /**
     * 属性的变化导致UI更新
     */
    public final ObservableField<UserAdapter> userListAdapter = new ObservableField<>();

    public final ObservableBoolean showContentList = new ObservableBoolean(false);

    UserAdapter userAdapter;


    public UserVm(UserActivity userActivity) {
        super(userActivity);
        userAdapter = new UserAdapter(t);
        userAdapter.setOnItemClickListener(onUserItemClick());
        userListAdapter.set(userAdapter);
    }

    @Command
    @DebugTrace(lever = LogLevel.ERROR)
    public void loadUsers() {
        /**
         * 由于界面显示的时候，easyRecycleView就会加载adapter，所以加载数据之前就要设置adapter，否则null pointer exception <br/>
         * 作者 ：zdj <br/>
         * created at 16-4-20 下午1:45
         */
        showLoading();
        /*userObserableBuilder.execute(new DefaultSubscribe<List<User>>() {
            @Override
            public void onNext(List<User> users) {
                MyLogUtil.e(TAG, "onNext: " + users.toString());
                userAdapter.addAll(mapper.more2more(users));
                showContentList();
            }

            @Override
            public void onCompleted() {
                super.onCompleted();
                MyLogUtil.e(TAG, "onCompleted: 被调用了");
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                MyLogUtil.e(TAG, "onError: " + e.toString());
                showRetry();
                t.showToast("请求失败");
                userAdapter.add(null);

            }
        });*/

    }

    private void showContentList() {
        showLoading.set(false);
        showRetry.set(false);
        showContentList.set(true);
    }

    @Command
    public void refreshUsers() {
        /*userObserableBuilder.execute(new DefaultSubscribe<List<User>>() {
            @Override
            public void onNext(List<User> users) {
                MyLogUtil.e(TAG, "onNext: " + users.toString());
                // 数据加载之后再清除，更好的体验
                userAdapter.clear();
                userAdapter.addAll(mapper.more2more(users));
                showContentList.set(true);
            }

            @Override
            public void onCompleted() {
                super.onCompleted();
                MyLogUtil.e(TAG, "onCompleted: 被调用了");
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                MyLogUtil.e(TAG, "onError: " + e.toString());
                t.showToast("请求失败");
                userAdapter.add(null);
            }
        });*/

    }

    @Override
    public void showRetry() {
        super.showRetry();
        showContentList.set(false);
    }

    public RecyclerArrayAdapter.OnItemClickListener onUserItemClick() {
        return (view, position) -> {
            Bundle bundle = new Bundle();
            bundle.putInt("userId", userAdapter.getItem(position).userId);
            toActivity(UserDetailActivity.class, bundle);
        };
    }


    @Override
    public View.OnClickListener onRetryClick() {
        return v -> {
            showRetry.set(false);
            showContentList.set(true);
            loadUsers();
        };
    }
}
